<?php

declare(strict_types=1);

namespace App\Admin\Controller\System\Permission;

use App\Admin\Annotation\Auth;
use App\Admin\Annotation\Permission;
use App\Admin\Request\System\Role\SystemRoleCreateRequest;
use App\Admin\Request\System\Role\SystemRoleStatusRequest;
use App\Admin\Service\System\SystemRoleService;
use DI\Attribute\Inject;
use Newbie\Annotation\Attributes\Route\Controller;
use Newbie\Annotation\Attributes\Route\DeleteMapping;
use Newbie\Annotation\Attributes\Route\GetMapping;
use Newbie\Annotation\Attributes\Route\PostMapping;
use Newbie\Annotation\Attributes\Route\PutMapping;
use Newbie\NewbieController;
use support\Request;
use support\Response;

/**
 * Class RoleController
 * @package App\system\Controller
 */
#[Controller(prefix: "system/role"), Auth]
class RoleController extends NewbieController {
    #[Inject]
    protected SystemRoleService $service;

    /**
     * 角色分页列表
     * @param Request $request
     * @return Response
     */
    #[GetMapping("index"), Permission("system:role:index")]
    public function index (Request $request): Response {
        return $this->success($this->service->getPageList($request->all()));
    }

    /**
     * 回收站角色分页列表
     * @param Request $request
     * @return Response
     */
    #[GetMapping("recycle"), Permission("system:role:recycle")]
    public function recycle (Request $request): Response {
        return $this->success($this->service->getPageListByRecycle($request->all()));
    }

    /**
     * 通过角色获取菜单
     * @param int $id
     * @return Response
     */
    #[GetMapping("getMenuByRole/{id}")]
    public function getMenuByRole (int $id): Response {
        return $this->success($this->service->getMenuByRole($id));
    }

    /**
     * 通过角色获取部门
     * @param int $id
     * @return Response
     */
    #[GetMapping("getDeptByRole/{id}")]
    public function getDeptByRole (int $id): Response {
        return $this->success($this->service->getDeptByRole($id));
    }

    /**
     * 获取角色列表 (不验证权限)
     * @return Response
     */
    #[GetMapping("list")]
    public function list (): Response {
        return $this->success($this->service->getList());
    }

    /**
     * 新增角色
     * @param SystemRoleCreateRequest $request
     * @return Response
     */
    #[PostMapping("save"), Permission("system:role:save")]
    public function save (SystemRoleCreateRequest $request): Response {
        return $this->success(['id' => $this->service->save($request->all())]);
    }

    /**
     * 更新角色
     * @param int $id
     * @param SystemRoleCreateRequest $request
     * @return Response
     */
    #[PutMapping("update/{id}"), Permission("system:role:update")]
    public function update (int $id, SystemRoleCreateRequest $request): Response {
        return $this->service->update($id, $request->all()) ? $this->success() : $this->error();
    }

    /**
     * 更新用户菜单权限
     * @param Request $request
     * @param int $id
     * @return Response
     */
    #[PutMapping("menuPermission/{id}"), Permission("system:role:menuPermission")]
    public function menuPermission (Request $request, int $id): Response {
        return $this->service->update($id, $request->all()) ? $this->success() : $this->error();
    }

    /**
     * 更新用户数据权限
     * @param Request $request
     * @param int $id
     * @return Response
     */
    #[PutMapping("dataPermission/{id}"), Permission("system:role:dataPermission")]
    public function dataPermission (Request $request, int $id): Response {
        return $this->service->update($id, $request->all()) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量删除数据到回收站
     * @param String $ids
     * @return Response
     */
    #[DeleteMapping("delete/{ids}"), Permission("system:role:delete")]
    public function delete (string $ids): Response {
        return $this->service->delete($ids) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量真实删除数据 （清空回收站）
     * @param String $ids
     * @return Response
     */
    #[DeleteMapping("realDelete/{ids}"), Permission("system:role:realDelete")]
    public function realDelete (string $ids): Response {
        return $this->service->realDelete($ids) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量恢复在回收站的数据
     * @param String $ids
     * @return Response
     */
    #[PutMapping("recovery/{ids}"), Permission("system:role:recovery")]
    public function recovery (string $ids): Response {
        return $this->service->recovery($ids) ? $this->success() : $this->error();
    }

    /**
     * 更改角色状态
     * @param SystemRoleStatusRequest $request
     * @return Response
     */
    #[PutMapping("changeStatus"), Permission("system:role:changeStatus")]
    public function changeStatus (SystemRoleStatusRequest $request): Response {
        return $this->service->changeStatus((int)$request->input('id'), (string)$request->input('status'))
            ? $this->success() : $this->error();
    }
}